<template>
  <div>
   <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddpage">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="goodsList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="100px"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="100px"></el-table-column>
        <el-table-column label="创建时间" width="200px" v-slot="{row}">
          {{row.add_time|dateFormat}}
        </el-table-column>
        <el-table-column label="操作" width="150px" v-slot="{row}">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="editGoodsA(row)"></el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeById(row)"></el-button>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <div class="block">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum" :page-sizes="[10, 30, 50, 100]" :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper" :total="total" background>
        </el-pagination>
      </div>
    </el-card>
    <!-- 编辑商品表单 -->
    <el-dialog title="编辑商品信息" :visible.sync="editDialogVisible" width="50%">
      <el-form :model="editGoods" :rules="editRuleForm" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="商品名称" prop="good_name">
          <el-input v-model="editRuleForm.good_name"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="goods_price">
          <el-input v-model="editRuleForm.goods_price"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 查询参数
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 商品列表数据
      goodsList: [],
      // 商品总条数
      total: 0,
      // 添加商品框弹出与关闭
      addDialogVisible: false,
      // 添加商品信息
      addGoods: {},
      // 编辑商品信息框弹出与关闭
      editDialogVisible: false,
      // 查询到的商品信息
      editGoods: {},
      editRuleForm: {
        good_name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        good_price: [
          { required: true, message: '请输入商品价格', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async getGoodsList() {
      const { data: res } = await this.axios.get('goods', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error('获取商品列表数据失败!')
      this.goodsList = res.data.goods
      this.total = res.data.total
      console.log(res.data)
    },
    // 分页事件
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    // 页码事件
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },
    // 添加商品
    goAddpage() {
      this.$router.push('/goods/add')
    },
    // 编辑商品信息事件
    editGoodsA(row) {
      this.editDialogVisible = true
    },
    // 删除商品
    async removeById(row) {
      const confirmResult = await this.$confirm('此操作将永久删除该用户,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除!')
      }
      const { data: res } = await this.axios.delete(`goods/${row.goods_id}`)
      if (res.meta.status !== 200) this.$message.error('删除商品失败!')
      this.getGoodsList()
      this.$message.success('删除商品成功!')
    }
  },
  created () {
    this.getGoodsList()
  },
  components: {}
}
</script>

<style lang="scss" scoped>
.block{
  margin-top: 10px;
}
</style>
